package site.wanjiahao.handler.server;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import site.wanjiahao.message.request.QuitGroupRequestMessage;
import site.wanjiahao.message.response.QuitGroupResponseMessage;
import site.wanjiahao.session.SessionFactory;

@ChannelHandler.Sharable
public class QuitGroupRequestMessageHandler extends SimpleChannelInboundHandler<QuitGroupRequestMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, QuitGroupRequestMessage msg) throws Exception {
        boolean isQuit = SessionFactory.getGroupSession().quit(msg.getGName(), ctx.channel());
        QuitGroupResponseMessage quitGroupResponseMessage = new QuitGroupResponseMessage(isQuit, isQuit ? "退出群聊成功" : "退出群聊失败");
        ctx.writeAndFlush(quitGroupResponseMessage);
    }
}
